package models

import (
	"encoding/json"
	"github.com/astaxie/beego/orm"
	"home/service"
)

// 定义结构
type HomeTrending struct{
	Id int
	Name string
	Number int
}

// 获取趋势
func GetTrendingList() ([]HomeTrending, error){
	var data []HomeTrending

	redis := service.Redis_tool{}
	redis.Init(14)
	redisKey := "home_trending"
	if redis.Bool(redisKey){
		reslut, err := redis.Get(redisKey)
		json.Unmarshal([]byte(reslut),&data)
		return  data,err
	}
	o := orm.NewOrm()
	_,err := o.Raw("select * from home_trending").QueryRows(&data)
	if b,err := json.Marshal(data); err == nil{
		redis.Set(redisKey, string(b))
		redis.Expire(redisKey,86400*7)
	}
	return data,err
}